<template>
  <div id="details">
    <div class="main">
      <div class="block">
        <div class="div-article">
          <!-- 文章 -->
          <el-card class="box-card">
            <div slot="header" class="clearfix">
              <span style="font-size: 25px">{{ article.articleTitle }}</span>
              <div style="margin-bottom: 5px; margin-top: 10px">
                <span style="margin-left: 10px"><i class="iconfont"
                                                   style="font-size: 15px">&#xe60a;</i> {{
                    article.commentCount
                  }}</span>
                <span style="margin-left: 10px"><i class="iconfont"
                                                   style="font-size: 15px">&#xe66b;</i> {{ article.likeCount }}</span>
                <span style="font-size: 15px; float: right">发表于 {{ article.createTime }}</span>
              </div>
            </div>
            <div class="div-content">
              <p class="p-contentHtml" v-html="article.contentHtml"></p>
            </div>
            <!-- 点赞 -->
            <el-divider>
              <i :class="['iconfont', isThumbs ? 'i-thumbs-after' : '']" style="font-size: 25px; cursor: pointer"
                 @click="giveThumbs">&#xe66b;</i>
            </el-divider>
            <!-- 点赞的用户头像 -->
            <div class="div-thumbs">
              <el-avatar :src="item.header" v-for="item in thumbsList"></el-avatar>
              <p style="font-size: 15px">共有 {{ thumbsCount }} 点赞</p>
            </div>
            <!-- 收藏 -->
            <el-divider>
              <i :class="['iconfont', isColl ? 'i-thumbs-after' :'']" style="font-size: 25px; cursor: pointer"
                 @click="collectionIt">&#xe8bc;</i>
            </el-divider>
            <!-- 收藏的用户头像 -->
            <div class="div-thumbs">
              <el-avatar :src="item.header" v-for="item in memberCollectList"></el-avatar>
              <p style="font-size: 15px">共有 {{ memberCollectCount }} 收藏</p>
            </div>
          </el-card>
        </div>
        <!-- 评论 -->
        <div class="div-comment">
          <el-card class="box-card">
            <div slot="header" class="clearfix">
              <h3>评论列表</h3>
              <div style="margin-top: 10px;" v-if="this.$store.getters.getUser.id != null">
                <el-row :gutter="20">
                  <el-col :span="4">
                    <div style="text-align: center">
                      <el-avatar :src="this.$store.getters.getUser.header"></el-avatar>
                    </div>
                  </el-col>
                  <el-col :span="20">
                    <el-input type="textarea" v-model="comment.content"></el-input>

                    <!-- 表情 -->
                    <el-popover placement="bottom"
                                title="标题"
                                width="500"
                                height="700"
                                trigger="click"
                                v-model="emojiShow">
                      <el-button style="margin-top: 10px" slot="reference" size="medium">😀</el-button>
                      <div class="browBox">
                        <ul>
                          <li v-for="(item, index) in faceList" :key="index" @click="getBrow(index)">
                            {{ item }}
                          </li>
                        </ul>
                      </div>
                    </el-popover>

                    <el-button type="primary" size="medium" style="margin-top: 10px; float: right" @click="submitComment">
                      发表评论
                    </el-button>
                  </el-col>
                </el-row>
              </div>
              <!-- 用户没有登录 -->
              <div v-else style="margin-top: 10px; height: 30px">
                <p style="text-align: center">
                  登录参与评论
                </p>
              </div>
            </div>
            <!-- 评论列表 -->
            <div class="div-comment-list" v-for="item in commentList">
              <el-row :gutter="20">
                <el-col :span="3">
                  <div style="text-align: center">
                    <el-avatar size="small" :src="item.header != null ? item.header : avatar"></el-avatar>
                  </div>
                </el-col>
                <el-col :span="21">
                  <div>
                    <p style="font-size: 15px; color: #99a9bf">{{ item.nickname }}</p>
                  </div>
                  <div style="margin-top: 10px">
                    <p v-html="item.content"></p>
                  </div>
                  <div style="margin-top: 10px; font-size: 13px" id="div-comment">
                    <span>{{item.createTime}}</span>
                    <span style="margin-left: 5px; cursor: pointer" @click="thumbsUpComment(item.id)">
                      <i class="iconfont" v-if="commentThumbsList.indexOf(item.id) == -1">&#xe8c3;</i>
                      <i class="iconfont i-comment-thumbs-after" v-else>&#xe8c3;</i>
                      <span :id="item.id"> {{item.thumbsCount}}</span>
                    </span>
                    <!-- 回复 -->
                    <span style="margin-left: 5px; cursor: pointer" @click="replyComment(item.id)">
                      回复 {{item.replyCount}}
                    </span>

                    <div style="margin-top: 10px;display: none" :id="'reply' + item.id">
                      <reply :commentId="item.id" :articleId="item.articleId" :replyIds="replyIdList"></reply>
                    </div>
                  </div>
                </el-col>
              </el-row>
              <el-divider></el-divider>
            </div>
            <!-- 分页 -->
            <el-pagination
                background
                :current-page="page"
                :page-size="limit"
                :total="total"
                style="padding: 30px 0; text-align: center;"
                layout="total, prev, pager, next, jumper"
                @current-change="getArticleComment(article.id)"
            />
          </el-card>
        </div>
      </div>

      <div class="content">
        <!-- 个人信息 -->
        <div class="div-person">
          <el-card class="box-card">
            <div style="text-align: center">
              <el-avatar :src="memberInfo.header"></el-avatar>
              <p>{{ memberInfo.nickname }}</p>
            </div>
            <el-divider></el-divider>
            <div class="div-collect">
              <el-row :gutter="20">
                <el-col :span="8">
                  <p style="font-size: 20px">{{ collectCount }}</p>
                  <p>收藏</p>
                </el-col>
                <el-col :span="8">
                  <p style="font-size: 20px">0</p>
                  <p>粉丝</p>
                </el-col>
                <el-col :span="8">
                  <p style="font-size: 20px">0</p>
                  <p>文章</p>
                </el-col>
              </el-row>
            </div>
            <el-divider></el-divider>
          </el-card>
        </div>
        <!-- 热帖 -->
        <div class="div-hotArticle">
          <el-card>
            <div slot="header" class="clearfix">
              <span style="font-family: 华文行楷; font-size: 30px">社区热门</span>
            </div>
            <div v-for="(item, index) in hotArticleList" :key="item.id">
              <div style="margin-top: 10px">
                <el-row :gutter="20">
                  <el-col :span="2">
                    <el-tag v-if="index == 0" type="danger" size="small">{{ index + 1 }}</el-tag>
                    <el-tag v-if="index == 1" size="small">{{ index + 1 }}</el-tag>
                    <el-tag v-if="index == 2" type="success" size="small">{{ index + 1 }}</el-tag>
                    <el-tag v-if="index > 2" type="info" size="small">{{ index + 1 }}</el-tag>
                  </el-col>
                  <el-col :span="22">
                    <router-link target="_blank" :to="{ path: '/club/details', query: {id:item.id} }">
                      <span style="font-size: 15px">{{ item.articleTitle }}</span>
                    </router-link>
                  </el-col>
                </el-row>
              </div>
            </div>
          </el-card>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import article from "@/api/club/article";
import articleComment from "@/api/club/articleComment";
import member from "@/api/member/member";
import memberArticleCollect from "@/api/member/memberArticleCollect";
import memberArticleThumbs from "@/api/member/memberArticleThumbs";
import memberCommentThumbs from "@/api/member/memberCommentThumbs";
import memberThumbsReply from "@/api/member/memberThumbsReply";
import Global from "@/Global";

export default {
  name: "ClubDetail",

  data() {
    return {
      article: {},
      avatar: 'https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png',
      commentList: [],
      memberInfo: {},
      hotArticleList: [],
      collectCount: 0,
      isThumbs: false,
      isColl: false,
      thumbsList: [],
      thumbsCount: 0,
      memberCollectList: [],
      memberCollectCount: 0,
      articleCollect: {},
      commentThumbsList: [],  //用户点赞的评论id
      replyIdList: [],  //用户点赞的回复id
      comment: {
        content: '',
      },  //评论信息
      showReply: false,
      emojiShow: false,  //表情框是否展示
      //表情列表
      faceList: [],
      //表情文本
      getBrowString: "",
      page: 1,
      limit: 10,
      total: 0,
    }
  },

  created() {
    //初始化页面内容
    this.init()
    //获取热帖
    this.getHotArticle()
    //加载表情
    this.loadEmojis();
  },

  methods: {
    //加载表情，存放到表情列表中
    loadEmojis() {
      for (let i in Global.appData) {
        this.faceList.push(Global.appData[i].char);
      }
    },

    getBrow(index) {
      for (let i in this.faceList) {
        if (index == i) {
          this.getBrowString = this.faceList[index];
          this.comment.content += this.getBrowString;
        }
      }
      this.emojiShow = false;
    },

    init() {
      if (this.$route.query.id != undefined) {
        const id = this.$route.query.id
        //获取文章信息
        this.getArticleById(id)
        //获取文章评论信息
        this.getArticleComment(id)
        //浏览数量+1
        this.increaseViewCount(id)
        //获取点赞该文章的用户
        this.getThumbsMember(id)
        //获取收藏该文章的用户
        this.getMemberCollect(id)

        //如果有登录用户，获取是否点赞、收藏等
        if (this.$store.getters.getUser) {
          memberArticleCollect.isCollectArticleInfo(id, this.$store.getters.getUser.id)
              .then(result => {
                const data = result.data
                if (1 == data) {
                  this.isColl = true
                }
              })

          memberArticleThumbs.isThumbsArticleInfo(id, this.$store.getters.getUser.id)
              .then(result => {
                const data = result.data
                if (1 == data) {
                  this.isThumbs = true
                }
              })

          memberCommentThumbs.getCommentThumbsInfoByMemberId(this.$store.getters.getUser.id)
              .then(result => {
                this.commentThumbsList = result.data
              })

          memberThumbsReply.getThumbsReplyIds(this.$store.getters.getUser.id, id)
          .then(result => {
            this.replyIdList = result.data
          })
        }
      }
    },

    getArticleById(id) {
      article.getArticleInfoById(id)
          .then(result => {
            this.article = result.data
            //获取博主信息
            member.getMemberInfoById(this.article.memberId)
                .then(result => {
                  this.memberInfo = result.data
                })

            //获取收藏数量
            memberArticleCollect.getCollectArticleInfoByMemberId(this.article.memberId)
                .then(result => {
                  this.collectCount = result.data
                })
          })
    },

    getArticleComment(id, page = 1) {
      this.page = page;
      const params = {
        page: this.page,
        articleId: id
      }
      articleComment.getArticleCommentInfo(params)
          .then(result => {
            const data = result.data
            this.commentList = data.list
            this.total = data.totalCount
          })
    },

    getHotArticle() {
      article.getHotArticleInfo()
          .then(result => {
            this.hotArticleList = result.data
          })
    },

    //获取点赞改文章的用户
    getThumbsMember(id) {
      memberArticleThumbs.getThumbsMemberInfo(id)
          .then(result => {
            const data = result.data
            this.thumbsList = data.thumbsList
            this.thumbsCount = data.count
          })
    },
    //获取收藏改文章的用户
    getMemberCollect(id) {
      memberArticleCollect.getMemberCollectInfo(id)
          .then(result => {
            const data = result.data
            this.memberCollectCount = data.count
            this.memberCollectList = data.collectList
          })
    },

    //点赞
    giveThumbs() {
      //判断用户是否登录
      if (!this.$store.getters.getUser) {
        this.$message({
          message: '请先登录',
          type: 'warning'
        })
        return
      }
      memberArticleThumbs.thumbsUpArticleByMemberId(this.$store.getters.getUser.id, this.article.id)
          .then(result => {
            this.isThumbs = !this.isThumbs
          })
    },

    //收藏
    collectionIt() {
      //判断用户是否登录
      if (!this.$store.getters.getUser) {
        this.$message({
          message: '请先登录',
          type: 'warning'
        })
        return
      }
      this.articleCollect.memberId = this.$store.getters.getUser.id
      this.articleCollect.articleId = this.article.id
      this.articleCollect.articleName = this.article.articleTitle
      this.articleCollect.articleDefImg = this.article.defImg
      memberArticleCollect.collectArticleInfo(this.articleCollect)
          .then(result => {
            this.isColl = !this.isColl
          })
    },

    //点赞评论
    thumbsUpComment(id) {
      //判断用户是否登录
      if (!this.$store.getters.getUser) {
        this.$message({
          message: '请先登录',
          type: 'warning'
        })
        return
      }

      memberCommentThumbs.thumbsUpCommentByMemberId(id, this.$store.getters.getUser.id)
          .then(result => {
            const data = result.data
            let ele = document.getElementById(id)
            let previousEle = ele.previousElementSibling
            if (0 == data) {
              previousEle.setAttribute("class", "iconfont")
              const value = parseInt(ele.innerText.trim()) - 1
              ele.innerText = ' ' + value
            } else {
              previousEle.setAttribute("class", "iconfont i-comment-thumbs-after")
              const value = parseInt(ele.innerText.trim()) + 1
              ele.innerText = ' ' + value
            }
          })
    },

    //浏览数量+1
    increaseViewCount(id) {
      article.increaseViewCountInfo(id).then(result => {
      })
    },

    //添加评论信息
    submitComment() {
      this.comment.memberId = this.$store.getters.getUser.id
      this.comment.articleId = this.article.id
      articleComment.saveArticleCommentInfo(this.comment)
          .then(result => {
            this.comment = {}  //清空
            //获取评论列表
            this.getArticleComment(this.article.id)
          })
    },

    //回复评论
    replyComment(id) {
      let ele = document.getElementById("reply" + id)
      let display = ele.style.display
      if ("none" === display) {
        ele.style.display = '';
      }else {
        ele.style.display = 'none';
      }
    }
  }
}
</script>

<style>
.browBox {
  width: 100%;
  height: 200px;
  background: #ffffff;
  position: absolute;
  z-index: 100;
  bottom: 0px;
  overflow: scroll;
}

.browBox ul {
  display: flex;
  flex-wrap: wrap;
  padding: 10px;
}

.browBox ul li {
  cursor: pointer;
  width: 10%;
  font-size: 20px;
  list-style: none;
  text-align: center;
}

.submit-btn {
  margin: 0 15px 10px 0;
  float: right;
}

/* 主要内容CSS */
#details .main {
  width: 1200px;
  height: 560px;
  padding-top: 30px;
  margin: 0 auto;
}

#details .main .block {
  float: left;
  width: 800px;
  padding-right: 10px;
}

#details .main .content {
  float: left;
  margin-left: 40px;
  width: 300px;
  /*padding-right: 10px;*/
}

.div-content {
  padding-left: 10px;
  padding-right: 10px;
}

.div-content p {
  text-indent: 20px;
  line-height: 30px;
  letter-spacing: 2px;
}

.p-contentHtml img {
  width: 500px;
  text-align: center;
}

.div-thumbs {
  text-align: center;
}

.div-comment {
  margin-top: 20px;
}

.div-comment-list {
  margin-bottom: 10px;
}

.div-collect {
  margin-top: 10px;
  text-align: center;
}

/**
社区热门
 */
.div-hotArticle {
  margin-top: 20px;
}

.i-comment-thumbs-after {
  color: #2a80ef;
}

.i-thumbs-after {
  color: #2a80ef;
}

</style>